/**
 * 
 */
package com.stulsoft.yssmugmugutils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


/**
 * @author Yuriy Stul
 *
 */
public class XmlUtils {

	/**
	 * Creates a new instance of the XML Document class.
	 * 
	 * @return the new instance of the XML Document class.
	 * @throws ParserConfigurationException
	 */
	public static Document createDocument() throws ParserConfigurationException {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document document = documentBuilder.newDocument();

		return document;
	}

	/**
	 * Saves a XML document into a file.
	 * 
	 * @param document
	 *            - the XML document to save.
	 * @param filePath
	 *            - the file location full path.
	 * @throws TransformerException
	 * @throws IOException
	 */
	public static void saveToFile(Document document, String filePath)
			throws TransformerException, IOException {
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();

		transformer.setOutputProperty("indent", "yes");
		DOMSource source = new DOMSource(document);
		OutputStream outputStream = new FileOutputStream(filePath);
		StreamResult result = new StreamResult(outputStream);
		transformer.transform(source, result);
		outputStream.close();
	}

	/**
	 * Loads XML file and returns a XML Document.
	 * 
	 * @param filePath
	 *            - XML file location.
	 * @return the XML Document.
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static Document getXmlFromFile(String filePath)
			throws ParserConfigurationException, SAXException, IOException {
		File file = new File(filePath);
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document document = documentBuilder.parse(file);
		document.getDocumentElement().normalize();

		return document;
	}

	/**
	 * Returns text representation of the XML document.
	 * 
	 * @param document
	 *            - the XML document to convert to string.
	 * @return the text representation of the XML document.
	 * @throws TransformerException
	 */
	public static String xmlToString(Document document)
			throws TransformerException {
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
		// "yes");
		StringWriter writer = new StringWriter();
		transformer
				.transform(new DOMSource(document), new StreamResult(writer));
		String output = writer.getBuffer().toString();

		return output;
	}
}
